package com.carleasoft.mps.cms.business.clerk.service.impl;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.clerk.bean.*;
import com.carleasoft.mps.cms.business.clerk.bean.po.*;
import com.carleasoft.mps.cms.business.clerk.bean.vo.*;
import com.carleasoft.mps.cms.business.clerk.constants.ProcessTypeEnum;
import com.carleasoft.mps.cms.business.clerk.dao.DealProcessMapper;
import com.carleasoft.mps.cms.business.clerk.service.*;
import com.carleasoft.mps.cms.business.personal.bean.entity.PersonalCenterEntity;
import com.carleasoft.mps.cms.business.personal.service.PersonalCenterService;
import com.carleasoft.mps.cms.business.train.bean.po.TrainConsumeItemPageQueryPo;
import com.carleasoft.mps.cms.business.train.bean.vo.TrainConsumeItemVo;
import com.carleasoft.mps.cms.business.train.constants.ExceptionTrainEnum;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.Student;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import com.carleasoft.mps.excel.utils.ConvertTemplatePath;
import com.carleasoft.mps.excel.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: yangHao
 * @createDate:2023/5/17
 * @description:
 */
@Service
public class DealProcessServiceImpl extends ServiceImpl<DealProcessMapper, DealProcessEntity> implements DealProcessService {

    @Autowired
    private DealProcessPaperService dealProcessPaperService;
    @Autowired
    private DealProcessGuideService dealProcessGuideService;
    @Autowired
    private DealProcessNodeService dealProcessNodeService;
    @Autowired
    private DealDataService dealDataService;
    @Autowired
    private DealRecordService dealRecordService;
    @Autowired
    private PersonalCenterService personalCenterService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDeal(DealProcessPo dealProcessPo, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        DealProcessEntity dealProcessEntity = BeanUtil.copyProperties(dealProcessPo, DealProcessEntity.class);
        dealProcessEntity.setProcessCode(IdWorker.getIdStr());
        dealProcessEntity.setIsDelete(0);
        dealProcessEntity.setConditionDesc(dealProcessPo.getCondition());
        dealProcessEntity.setCreateTime(LocalDateTime.now());
        dealProcessEntity.setUpdateTime(LocalDateTime.now());
        dealProcessEntity.setCreatePersonCode(user.getUserCode());
        dealProcessEntity.setCreatePersonName(user.getUserName());
        dealProcessEntity.setUpdatePersonCode(user.getUserCode());
        dealProcessEntity.setUpdatePersonName(user.getUserName());
        isOk = this.save(dealProcessEntity);
        // 添加申请材料集合
        if (CollUtil.isNotEmpty(dealProcessPo.getDealProcessPaperEntities())) {
            for (DealProcessPaperEntity dealProcessPaperEntity : dealProcessPo.getDealProcessPaperEntities()) {
                dealProcessPaperEntity.setPaperCode(IdWorker.getIdStr());
                dealProcessPaperEntity.setProcessCode(dealProcessEntity.getProcessCode());
            }
            isOk = dealProcessPaperService.saveBatch(dealProcessPo.getDealProcessPaperEntities());
        }
        // 添加办公指引集合
        if (CollUtil.isNotEmpty(dealProcessPo.getDealProcessGuideEntities())) {
            for (DealProcessGuideEntity dealProcessGuideEntity : dealProcessPo.getDealProcessGuideEntities()) {

                dealProcessGuideEntity.setProcessCode(dealProcessEntity.getProcessCode());
            }
            isOk = dealProcessGuideService.saveBatch(dealProcessPo.getDealProcessGuideEntities());
        }
        // 添加流程定义
        for (DealProcessNodeEntity dealProcessNodeEntity : dealProcessPo.getDealProcessNodeEntities()) {

            dealProcessNodeEntity.setProcessCode(dealProcessEntity.getProcessCode());
            dealProcessNodeEntity.setNodeCode(IdWorker.getIdStr());
        }
        isOk = dealProcessNodeService.saveBatch(dealProcessPo.getDealProcessNodeEntities());

        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDeal(DealProcessPo dealProcessPo, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        DealProcessEntity processEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery()
                .eq(DealProcessEntity::getProcessCode, dealProcessPo.getProcessCode())
                .eq(DealProcessEntity::getIsDelete, 0));
        BeanUtil.copyProperties(dealProcessPo, processEntity);
        processEntity.setConditionDesc(dealProcessPo.getCondition());
        processEntity.setUpdateTime(LocalDateTime.now());
        processEntity.setUpdatePersonCode(user.getUserCode());
        processEntity.setUpdatePersonName(user.getUserName());
        isOk = this.updateById(processEntity);
        // 添加申请材料集合
        if (CollUtil.isNotEmpty(dealProcessPo.getDealProcessPaperEntities())) {
            List<DealProcessPaperEntity> dealProcessPaperEntities = dealProcessPaperService.list(Wrappers.<DealProcessPaperEntity>lambdaQuery()
                    .eq(DealProcessPaperEntity::getProcessCode, dealProcessPo.getProcessCode()));
            if (CollUtil.isNotEmpty(dealProcessPaperEntities)) {
                List<Long> ids = dealProcessPaperEntities.stream().map(DealProcessPaperEntity::getId).collect(Collectors.toList());
                isOk = dealProcessPaperService.removeByIds(ids);
            }
            for (DealProcessPaperEntity dealProcessPaperEntity : dealProcessPo.getDealProcessPaperEntities()) {

                dealProcessPaperEntity.setProcessCode(processEntity.getProcessCode());
            }
            isOk = dealProcessPaperService.saveBatch(dealProcessPo.getDealProcessPaperEntities());
        }

        // 添加办公指引集合
        if (CollUtil.isNotEmpty(dealProcessPo.getDealProcessGuideEntities())) {
            List<DealProcessGuideEntity> dealProcessGuideEntities = dealProcessGuideService.list(Wrappers.<DealProcessGuideEntity>lambdaQuery()
                    .eq(DealProcessGuideEntity::getProcessCode, dealProcessPo.getProcessCode()));
            if (CollUtil.isNotEmpty(dealProcessGuideEntities)) {
                List<Long> ids = dealProcessGuideEntities.stream().map(DealProcessGuideEntity::getId).collect(Collectors.toList());
                isOk = dealProcessGuideService.removeByIds(ids);
            }
            for (DealProcessGuideEntity dealProcessGuideEntity : dealProcessPo.getDealProcessGuideEntities()) {

                dealProcessGuideEntity.setProcessCode(processEntity.getProcessCode());
            }
            isOk = dealProcessGuideService.saveBatch(dealProcessPo.getDealProcessGuideEntities());
        }
        // 添加流程定义
        List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery().eq(DealProcessNodeEntity::getProcessCode, dealProcessPo.getProcessCode())
                .isNull(DealProcessNodeEntity::getRecordCode));
        List<Long> ids = dealProcessNodeEntities.stream().map(DealProcessNodeEntity::getId).collect(Collectors.toList());
        isOk = dealProcessNodeService.removeByIds(ids);
        for (DealProcessNodeEntity dealProcessNodeEntity : dealProcessPo.getDealProcessNodeEntities()) {
            dealProcessNodeEntity.setNodeCode(IdWorker.getIdStr());
            dealProcessNodeEntity.setProcessCode(processEntity.getProcessCode());
        }
        isOk = dealProcessNodeService.saveBatch(dealProcessPo.getDealProcessNodeEntities());


        return isOk;
    }


    @Override
    public DealProcessPo detail(DealProcessPo dealProcessPo) {
        DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery()
                .eq(DealProcessEntity::getProcessCode, dealProcessPo.getProcessCode())
                .eq(DealProcessEntity::getIsDelete, 0));
        DealProcessPo processPo = BeanUtil.copyProperties(dealProcessEntity, DealProcessPo.class);
        processPo.setCondition(dealProcessEntity.getConditionDesc());
        List<DealProcessPaperEntity> dealProcessPaperEntities = dealProcessPaperService.list(Wrappers.<DealProcessPaperEntity>lambdaQuery()
                .eq(DealProcessPaperEntity::getProcessCode, dealProcessPo.getProcessCode()));
        if (CollUtil.isNotEmpty(dealProcessPaperEntities)) {
            processPo.setDealProcessPaperEntities(dealProcessPaperEntities);
        }
        List<DealProcessGuideEntity> dealProcessGuideEntities = dealProcessGuideService.list(Wrappers.<DealProcessGuideEntity>lambdaQuery()
                .eq(DealProcessGuideEntity::getProcessCode, dealProcessPo.getProcessCode()));
        if (CollUtil.isNotEmpty(dealProcessGuideEntities)) {
            processPo.setDealProcessGuideEntities(dealProcessGuideEntities);
        }
        //todo 查询当前登录人是否有添加过数据，加过数据得过滤旧数据
        List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                .eq(DealProcessNodeEntity::getProcessCode, dealProcessPo.getProcessCode())
                .isNull(DealProcessNodeEntity::getRecordCode));
        processPo.setDealProcessNodeEntities(dealProcessNodeEntities);
        return processPo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean stopAndStart(StopAndStartPo stopAndStartPo) {
        DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery()
                .eq(DealProcessEntity::getProcessCode, stopAndStartPo.getProcessCode())
                .eq(DealProcessEntity::getIsDelete, 0));
        if (Objects.isNull(dealProcessEntity)) {
            throw new BusinessException(ExceptionCMSEnum.DATABASE_NO);
        }
        dealProcessEntity.setState(stopAndStartPo.getType());
        return this.updateById(dealProcessEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(DealProcessPo dealProcessPo) {
        DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery()
                .eq(DealProcessEntity::getProcessCode, dealProcessPo.getProcessCode())
                .eq(DealProcessEntity::getIsDelete, 0));
        if (dealProcessEntity.getState().equals(1)) {
            throw new BusinessException(ExceptionCMSEnum.STATE_NO);
        }
        dealProcessEntity.setIsDelete(1);
        return this.updateById(dealProcessEntity);
    }

    @Override
    public Page<PageQueryVo> pageQuery(Page page, PageQueryPo pageQueryPo) {
        Page<PageQueryVo> pageQueryVoPage = this.getBaseMapper().pageQuery(page, pageQueryPo);

        if (CollUtil.isNotEmpty(pageQueryVoPage.getRecords())) {
            for (PageQueryVo record : pageQueryVoPage.getRecords()) {
                if (record.getState() != 0) {
                    record.setProcessTypeName(ProcessTypeEnum.getPaymentType(record.getProcessType()));

                }
            }
        }
        return pageQueryVoPage;
    }

    @Override
    public Page<PageQueryVo> doorPageQuery(Page page, PageQueryPo pageQueryPo) {
        Page<PageQueryVo> pageQueryVoPage = new Page<>();
        if (pageQueryPo.getHeat() != null && pageQueryPo.getHeat() == 1) {
            pageQueryVoPage = this.getBaseMapper().doorPageQuery(page, pageQueryPo);

        } else {
            pageQueryVoPage = this.getBaseMapper().pageQuery(page, pageQueryPo);

        }
        if (CollUtil.isNotEmpty(pageQueryVoPage.getRecords()) && pageQueryPo.getHeat() == null) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            String monthNew = null;
            if (month < 10) {
                monthNew = year + "-" + "0" + month;
            } else {
                monthNew = year + "-" + month;
            }
            // 过滤只查当月的
            List<DealProcessNodeEntity> dealDataEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                    .like(DealProcessNodeEntity::getCreateTime, monthNew));
            Map<String, List<DealProcessNodeEntity>> map = new HashMap<>();
            if (CollUtil.isNotEmpty(dealDataEntities)) {
                map = dealDataEntities.stream().collect(Collectors.groupingBy(DealProcessNodeEntity::getRecordCode));
            }
            // 查询所有
            List<DealProcessNodeEntity> dataEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                    .isNotNull(DealProcessNodeEntity::getRecordCode));
            Map<String, List<DealProcessNodeEntity>> hasMap = new HashMap<>();
            if (CollUtil.isNotEmpty(dataEntities)) {
                hasMap = dataEntities.stream().collect(Collectors.groupingBy(DealProcessNodeEntity::getRecordCode));
            }
            List<DealDataEntity> entities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                    .isNotNull(DealDataEntity::getRecordCode));
            Map<String, List<DealDataEntity>> listMap = entities.stream().collect(Collectors.groupingBy(DealDataEntity::getProcessCode));
            for (PageQueryVo record : pageQueryVoPage.getRecords()) {
                if (listMap.containsKey(record.getProcessCode())) {
                    List<DealDataEntity> entityList = listMap.get(record.getProcessCode());
                    Set<String> collect = entityList.stream().distinct().map(DealDataEntity::getRecordCode).collect(Collectors.toSet());
                    for (String s : collect) {
                        if (map.containsKey(s)) {
                            record.setMonthNumber(record.getMonthNumber() + 1);
                        }
                        if (hasMap.containsKey(s)) {
                            record.setSum(record.getSum() + 1);
                        }
                    }


                }
            }
        }
        return pageQueryVoPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean doorSubmit(DoorSubmitPo doorSubmitPo, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();

        List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                .eq(DealProcessNodeEntity::getProcessCode, doorSubmitPo.getProcessCode())
                .isNull(DealProcessNodeEntity::getRecordCode));
        String idStr = IdWorker.getIdStr();
        DealRecordEntity dealRecordEntity = new DealRecordEntity();
        dealRecordEntity.setMessageType("个人申请");
        dealRecordEntity.setCreateTime(LocalDateTime.now());
        dealRecordEntity.setCreatePersonCode(user.getUserCode());
        dealRecordEntity.setCreatePersonName(user.getUserName());
        dealRecordEntity.setRecordCode(doorSubmitPo.getProcessCode());
        dealRecordEntity.setRecordCode(idStr);
        isOk = dealRecordService.save(dealRecordEntity);
        List<DealProcessNodeEntity> processNodeEntities = new ArrayList<>();
        for (DealProcessNodeEntity dealProcessNodeEntity : dealProcessNodeEntities) {
            DealProcessNodeEntity processNodeEntity = BeanUtil.copyProperties(dealProcessNodeEntity, DealProcessNodeEntity.class);
            processNodeEntity.setRecordCode(idStr);
            processNodeEntity.setCreateTime(LocalDateTime.now());
            processNodeEntities.add(processNodeEntity);
        }
        List<DealProcessNodeEntity> nodeEntities = processNodeEntities.stream().sorted(Comparator.comparing(DealProcessNodeEntity::getNodeOrder)).collect(Collectors.toList());
        isOk = dealProcessNodeService.saveBatch(nodeEntities);

        int aaa = -1;
        for (int i = 0; i < nodeEntities.size(); i++) {
            DealProcessNodeEntity dealProcessNodeEntity = nodeEntities.get(i);
            if (dealProcessNodeEntity.getIsApprove().equals(1)) {
                aaa = i;
                break;
            }
        }
        if (aaa == -1) {
            DealProcessNodeEntity dealProcessNodeEntity = nodeEntities.get(0);
            DealDataEntity dealDataEntity = new DealDataEntity();
            dealDataEntity.setCreatePersonName(user.getUserName());
            dealDataEntity.setCreatePersonCode(user.getUserCode());
            dealDataEntity.setCreateTime(LocalDateTime.now());
            dealDataEntity.setCode(IdWorker.getIdStr());
            dealDataEntity.setRecordCode(idStr);
            dealDataEntity.setState(4);
            dealDataEntity.setProcessCode(dealProcessNodeEntity.getProcessCode());
            dealDataEntity.setNodeCode(dealProcessNodeEntity.getNodeCode());
            isOk = dealDataService.save(dealDataEntity);
        } else {
            DealProcessNodeEntity dealProcessNodeEntity = nodeEntities.get(aaa);
            DealDataEntity dealDataEntity = new DealDataEntity();
            dealDataEntity.setCreatePersonName(user.getUserName());
            dealDataEntity.setCreatePersonCode(user.getUserCode());
            dealDataEntity.setCreateTime(LocalDateTime.now());
            dealDataEntity.setCode(IdWorker.getIdStr());
            dealDataEntity.setRecordCode(idStr);
            dealDataEntity.setState(1);
            dealDataEntity.setProcessCode(dealProcessNodeEntity.getProcessCode());
            dealDataEntity.setNodeCode(dealProcessNodeEntity.getNodeCode());
            isOk = dealDataService.save(dealDataEntity);
            DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery().eq(DealProcessEntity::getProcessCode, doorSubmitPo.getProcessCode()));
            PersonalCenterEntity personalCenterEntity = new PersonalCenterEntity();
            personalCenterEntity.setMessage("您有一条" + dealProcessEntity.getProcessName() + "待处置,点击查看");
            personalCenterEntity.setResourceType(1);
            personalCenterEntity.setType(1);
            personalCenterEntity.setCode(dealDataEntity.getCode());
            personalCenterEntity.setUserCode(dealProcessNodeEntity.getApproverCode());
            isOk = personalCenterService.savePersonalCenter(personalCenterEntity);
        }


        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(DoorSubmitPo doorSubmitPo, HttpServletRequest request) {
        return false;
    }

    @Override
    public Page<DisposePageVo> disposePageQuery(Page page, DisposePagePo disposePagePo, HttpServletRequest request) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        disposePagePo.setUserCode(user.getUserCode());
        Page<DisposePageVo> disposePageVoPage = this.getBaseMapper().disposePageQuery(page, disposePagePo);
        if (CollUtil.isNotEmpty(disposePageVoPage.getRecords())) {
            Set<String> codes = disposePageVoPage.getRecords().stream().map(DisposePageVo::getCode).collect(Collectors.toSet());
            List<DealDataEntity> dealDataEntities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                    .in(DealDataEntity::getCode, codes)
                    .eq(DealDataEntity::getNodeState, 1));
            Map<String, List<DealDataEntity>> map = new HashMap<>();
            if (CollUtil.isNotEmpty(dealDataEntities)) {
                map = dealDataEntities.stream().collect(Collectors.groupingBy(DealDataEntity::getRecordCode));
            }
            for (DisposePageVo record : disposePageVoPage.getRecords()) {
                record.setProcessTypeName(ProcessTypeEnum.getPaymentType(record.getProcessType()));
                if (record.getNodeState() != null && record.getNodeState() == 1 && map.containsKey(record.getRecordCode()) && record.getState() != 4) {
                    List<DealDataEntity> dataEntities = map.get(record.getRecordCode());
                    DealDataEntity dealDataEntity = dataEntities.get(dataEntities.size() - 1);
                    if (record.getCode().equals(dealDataEntity.getCode())) {
                        DealDataEntity one = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                                .eq(DealDataEntity::getCode, dealDataEntity.getCode()));
                        DealProcessNodeEntity nodeServiceOne = dealProcessNodeService.getOne(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                                .eq(DealProcessNodeEntity::getRecordCode, one.getRecordCode())
                                .eq(DealProcessNodeEntity::getProcessCode, one.getProcessCode())
                                .eq(DealProcessNodeEntity::getNodeCode, one.getNodeCode()));
                        List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                                .eq(DealProcessNodeEntity::getRecordCode, one.getRecordCode())
                                .eq(DealProcessNodeEntity::getProcessCode, one.getProcessCode())
                                .gt(DealProcessNodeEntity::getNodeOrder, nodeServiceOne.getNodeOrder())
                                .isNotNull(DealProcessNodeEntity::getApproverCode));
                        if (CollUtil.isNotEmpty(dealProcessNodeEntities)) {
                            DealProcessNodeEntity dealProcessNodeEntity = dealProcessNodeEntities.get(0);
                            DealDataEntity dealDataServiceOne = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                                    .eq(DealDataEntity::getNodeCode, dealProcessNodeEntity.getNodeCode())
                                    .eq(DealDataEntity::getProcessCode, dealProcessNodeEntity.getProcessCode())
                                    .eq(DealDataEntity::getRecordCode, dealProcessNodeEntity.getRecordCode()));
                            if (Objects.nonNull(dealDataServiceOne) && dealDataServiceOne.getNodeState() != null && dealDataServiceOne.getNodeState() == 1) {
                                record.setRevocation(1);
                            } else {
                                record.setRevocation(2);
                            }
                        } else {
                            record.setRevocation(2);

                        }
                    } else {
                        record.setRevocation(1);
                    }
                } else {
                    record.setRevocation(1);
                }
            }
        }
        return disposePageVoPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disposeSave(DisposePagePo disposePagePo, HttpServletRequest request) {
        boolean isOk = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        DealDataEntity dealDataServiceOne = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                .eq(DealDataEntity::getCode, disposePagePo.getCode()));
        dealDataServiceOne.setCreateTime(LocalDateTime.now());
        dealDataServiceOne.setNodeState(disposePagePo.getNodeState());
        dealDataServiceOne.setDataDesc(disposePagePo.getDataDesc());
        isOk = dealDataService.updateById(dealDataServiceOne);
        if (isOk) {

            PersonalCenterEntity personalCenterServiceOne = personalCenterService.getOne(Wrappers.<PersonalCenterEntity>lambdaQuery()
                    .eq(PersonalCenterEntity::getCode, disposePagePo.getCode())
                    .like(PersonalCenterEntity::getUserCode, user.getUserCode()));
            if (Objects.nonNull(personalCenterServiceOne)) {
                personalCenterService.remove(Wrappers.<PersonalCenterEntity>lambdaQuery()
                        .eq(PersonalCenterEntity::getCode, personalCenterServiceOne.getCode()));
            }
        }
        if (disposePagePo.getNodeState() == 2) {
            // 修改主流程为不通过
            isOk = dealDataService.update(Wrappers.<DealDataEntity>lambdaUpdate()
                    .eq(DealDataEntity::getRecordCode, dealDataServiceOne.getRecordCode())
                    .set(DealDataEntity::getState, 3));
            DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery().eq(DealProcessEntity::getProcessCode, dealDataServiceOne.getProcessCode()));
            PersonalCenterEntity personalCenterEntity = new PersonalCenterEntity();
            personalCenterEntity.setMessage("您有一条" + dealProcessEntity.getProcessName() + " 已处置,点击查看");
            personalCenterEntity.setResourceType(2);
            personalCenterEntity.setType(1);
            personalCenterEntity.setCode(dealDataServiceOne.getCode());
            personalCenterEntity.setUserCode(dealDataServiceOne.getCreatePersonCode());
            isOk = personalCenterService.savePersonalCenter(personalCenterEntity);


        }
        if (disposePagePo.getNodeState() == 1) {
            List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                    .eq(DealProcessNodeEntity::getRecordCode, dealDataServiceOne.getRecordCode()));
            int aaa = 0;
            DealDataEntity dealDataEntity = new DealDataEntity();
            for (int i = 0; i < dealProcessNodeEntities.size(); i++) {
                DealProcessNodeEntity dealProcessNodeEntity = dealProcessNodeEntities.get(i);
                if (dealProcessNodeEntity.getNodeCode().equals(dealDataServiceOne.getNodeCode())) {
                    aaa = 1;
                }
                if (aaa == 1) {
                    if (i + 1 > dealProcessNodeEntities.size() - 1) {
                        aaa = 2;
                        break;
                    }
                    DealProcessNodeEntity processNodeEntity = dealProcessNodeEntities.get(i + 1);
                    if (processNodeEntity.getIsApprove().equals(1)) {
                        dealDataEntity.setCode(IdWorker.getIdStr());
                        dealDataEntity.setRecordCode(dealDataServiceOne.getRecordCode());
                        dealDataEntity.setProcessCode(processNodeEntity.getProcessCode());
                        dealDataEntity.setNodeCode(processNodeEntity.getNodeCode());
                        dealDataEntity.setState(1);
                        dealDataEntity.setCreatePersonCode(dealDataServiceOne.getCreatePersonCode());
                        dealDataEntity.setCreatePersonName(dealDataServiceOne.getCreatePersonName());
                        DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery().eq(DealProcessEntity::getProcessCode, processNodeEntity.getProcessCode()));
                        PersonalCenterEntity personalCenterEntity = new PersonalCenterEntity();
                        personalCenterEntity.setMessage("您有一条" + dealProcessEntity.getProcessName() + " 待处置,点击查看");
                        personalCenterEntity.setResourceType(1);
                        personalCenterEntity.setType(1);
                        personalCenterEntity.setCode(dealDataEntity.getCode());
                        personalCenterEntity.setUserCode(processNodeEntity.getApproverCode());
                        isOk = personalCenterService.savePersonalCenter(personalCenterEntity);
                        break;
                    }
                }
            }
            if (StrUtil.isNotBlank(dealDataEntity.getCode()) && aaa == 1) {
                isOk = dealDataService.save(dealDataEntity);
            }

            if (aaa == 2 && disposePagePo.getNodeState() == 1) {
                // 修改主流程为已结束
                isOk = dealDataService.update(Wrappers.<DealDataEntity>lambdaUpdate()
                        .eq(DealDataEntity::getRecordCode, dealDataServiceOne.getRecordCode())
                        .set(DealDataEntity::getState, 4));
                DealProcessEntity dealProcessEntity = this.getOne(Wrappers.<DealProcessEntity>lambdaQuery().eq(DealProcessEntity::getProcessCode, dealDataServiceOne.getProcessCode()));
                PersonalCenterEntity personalCenterEntity = new PersonalCenterEntity();
                personalCenterEntity.setMessage("您有一条" + dealProcessEntity.getProcessName() + " 已处置,点击查看");
                personalCenterEntity.setResourceType(2);
                personalCenterEntity.setType(1);
                personalCenterEntity.setUserCode(dealDataServiceOne.getCreatePersonCode());
                personalCenterEntity.setCode(disposePagePo.getCode());
                isOk = personalCenterService.savePersonalCenter(personalCenterEntity);
            }
        }
        DealProcessNodeEntity dealProcessNodeServiceOne = dealProcessNodeService.getOne(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                .eq(DealProcessNodeEntity::getProcessCode, dealDataServiceOne.getProcessCode())
                .eq(DealProcessNodeEntity::getNodeCode, dealDataServiceOne.getNodeCode())
                .eq(DealProcessNodeEntity::getRecordCode, dealDataServiceOne.getRecordCode()));

        DealRecordEntity dealRecordServiceOne = new DealRecordEntity();
        dealRecordServiceOne.setCreateTime(LocalDateTime.now());
        dealRecordServiceOne.setCreatePersonCode(user.getUserCode());
        dealRecordServiceOne.setCreatePersonName(user.getUserName());
        dealRecordServiceOne.setMessageType(dealProcessNodeServiceOne.getNodeName());
        dealRecordServiceOne.setContent(disposePagePo.getDataDesc());
        if (disposePagePo.getNodeState() == 1) {
            dealRecordServiceOne.setStatus("通过");
        } else {
            dealRecordServiceOne.setStatus("不通过");
        }
        dealRecordServiceOne.setRecordCode(dealDataServiceOne.getRecordCode());
        dealRecordServiceOne.setProcessCode(dealDataServiceOne.getProcessCode());
        dealRecordServiceOne.setNodeCode(dealDataServiceOne.getNodeCode());
        isOk = dealRecordService.save(dealRecordServiceOne);

        return isOk;
    }

    @Override
    public DisposeDetailVo disposeDetail(DisposeDetailPo disposeDetailPo) {
        DealDataEntity dealDataServiceOne = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                .eq(DealDataEntity::getCode, disposeDetailPo.getCode()));
        DisposeDetailVo disposeDetailVo = new DisposeDetailVo();
        disposeDetailVo.setNodeState(dealDataServiceOne.getNodeState());
        disposeDetailVo.setDataDesc(dealDataServiceOne.getDataDesc());
        return disposeDetailVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean revocation(RevocationPo revocationPo) {
        boolean isOk = false;
        DealDataEntity dealDataServiceOne = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                .eq(DealDataEntity::getCode, revocationPo.getCode()));
        if (dealDataServiceOne.getNodeState().equals(1)) {
            DealDataEntity dataServiceOne = dealDataService.getOne(Wrappers.<DealDataEntity>lambdaQuery()
                    .eq(DealDataEntity::getRecordCode, dealDataServiceOne.getRecordCode())
                    .isNull(DealDataEntity::getNodeState));
            if (Objects.nonNull(dataServiceOne)) {
                isOk = dealDataService.removeById(dataServiceOne.getId());
            }
            isOk = dealDataService.update(Wrappers.<DealDataEntity>lambdaUpdate()
                    .eq(DealDataEntity::getCode, dealDataServiceOne.getCode())
                    .set(DealDataEntity::getDataDesc, null)
                    .set(DealDataEntity::getNodeState, null)
                    .set(DealDataEntity::getState, 1));

        }
        return isOk;
    }

    @Override
    public List<DealRecordEntity> record(DealProcessPo dealProcessPo) {
        return dealRecordService.list(Wrappers.<DealRecordEntity>lambdaQuery()
                .eq(DealRecordEntity::getRecordCode, dealProcessPo.getRecordCode()).orderByAsc(DealRecordEntity::getCreateTime));
    }

    @Override
    public List<CategoryDoorVo> categoryDoor() {
        List<CategoryDoorVo> categoryDoorVos = new ArrayList<>();
        CategoryDoorVo categoryDoorVo = new CategoryDoorVo();
        categoryDoorVo.setProcessType(0);
        categoryDoorVo.setProcessTypeName("全部");
        categoryDoorVos.add(categoryDoorVo);
        for (ProcessTypeEnum value : ProcessTypeEnum.values()) {
            CategoryDoorVo categoryDoorVoss = new CategoryDoorVo();
            categoryDoorVoss.setProcessType(value.getCode());
            categoryDoorVoss.setProcessTypeName(value.getMessage());
            categoryDoorVos.add(categoryDoorVoss);
        }
        if (CollUtil.isNotEmpty(categoryDoorVos)) {
            List<DealProcessEntity> dealProcessEntities = this.list(Wrappers.<DealProcessEntity>lambdaQuery()
                    .eq(DealProcessEntity::getState, 1));
            if (CollUtil.isNotEmpty(dealProcessEntities)) {
                Map<Integer, List<DealProcessEntity>> map = dealProcessEntities.stream().collect(Collectors.groupingBy(DealProcessEntity::getProcessType));
                for (CategoryDoorVo categoryDoorVosss : categoryDoorVos) {
                    if (map.containsKey(categoryDoorVosss.getProcessType())) {
                        categoryDoorVosss.setSum(map.get(categoryDoorVosss.getProcessType()).size());
                    }
                }
            }

        }
        int sum = categoryDoorVos.stream().mapToInt(CategoryDoorVo::getSum).sum();
        categoryDoorVos.get(0).setSum(sum);
        return categoryDoorVos.stream().sorted(Comparator.comparing(CategoryDoorVo::getSum).reversed()).collect(Collectors.toList());

    }

    @Override
    public DataCountVo dataCount() {
        DataCountVo dataCountVo = new DataCountVo();
        List<DealDataEntity> dealDataEntities = dealDataService.list();
        if (CollUtil.isNotEmpty(dealDataEntities)) {
            Map<String, List<DealDataEntity>> map = dealDataEntities.stream().collect(Collectors.groupingBy(DealDataEntity::getRecordCode));
            Set<String> collect = dealDataEntities.stream().map(DealDataEntity::getRecordCode).collect(Collectors.toSet());
            dataCountVo.setSum(map.size());
            int aaa = 0;
            for (String s : collect) {
                List<DealDataEntity> dataEntities = map.get(s);
                List<Integer> integers = dataEntities.stream().map(DealDataEntity::getState).collect(Collectors.toList());
                if (integers.contains(4)) {
                    aaa = aaa + 1;
                }
            }
            dataCountVo.setDispose(aaa);
            if (aaa != 0) {
                BigDecimal bigDecimal = new BigDecimal(aaa);
                BigDecimal decimal = new BigDecimal(map.size());
                BigDecimal divide = bigDecimal.divide(decimal, 4, BigDecimal.ROUND_HALF_UP);
                BigDecimal multiply = divide.multiply(new BigDecimal(100));
                double value = multiply.doubleValue();
                dataCountVo.setRate(value + "%");
            } else {
                dataCountVo.setRate(0 + "%");
            }
        }
        return dataCountVo;
    }

    @Override
    public List<RankingVo> ranking() {
        List<RankingVo> rankingVos = new ArrayList<>();
        List<DealDataEntity> dealDataEntities = dealDataService.list();
        if (CollUtil.isNotEmpty(dealDataEntities)) {
            Map<String, List<DealDataEntity>> map = dealDataEntities.stream().collect(Collectors.groupingBy(DealDataEntity::getProcessCode));
            Set<String> collect = dealDataEntities.stream().map(DealDataEntity::getProcessCode).collect(Collectors.toSet());
            List<DealProcessEntity> dealProcessEntities = this.list(Wrappers.<DealProcessEntity>lambdaQuery()
                    .in(DealProcessEntity::getProcessCode, collect));
            Map<String, DealProcessEntity> entityMap = dealProcessEntities.stream().collect(Collectors.toMap(DealProcessEntity::getProcessCode, Function.identity()));
            for (String s : collect) {
                RankingVo rankingVo = new RankingVo();
                rankingVo.setProcessName(entityMap.get(s).getProcessName());
                List<DealDataEntity> dataEntities = map.get(s);
                Set<String> set = dataEntities.stream().map(DealDataEntity::getRecordCode).collect(Collectors.toSet());
                Map<String, List<DealDataEntity>> listMap = dataEntities.stream().collect(Collectors.groupingBy(DealDataEntity::getRecordCode));
                rankingVo.setSum(listMap.size());
                int aaa = 0;
                for (String s1 : set) {
                    List<DealDataEntity> entities = listMap.get(s1);
                    List<Integer> integers = entities.stream().map(DealDataEntity::getState).collect(Collectors.toList());
                    if (integers.contains(4)) {
                        aaa = aaa + 1;
                    }
                }
                rankingVo.setDispose(aaa);
                if (aaa != 0) {
                    BigDecimal bigDecimal = new BigDecimal(aaa);
                    BigDecimal decimal = new BigDecimal(rankingVo.getSum());
                    BigDecimal divide = bigDecimal.divide(decimal, 4, BigDecimal.ROUND_HALF_UP);
                    BigDecimal multiply = divide.multiply(new BigDecimal(100));
                    double value = multiply.doubleValue();
                    rankingVo.setRateCopy(multiply);
                    rankingVo.setRate(value + "%");
                } else {
                    rankingVo.setRate(0 + "%");
                    rankingVo.setRateCopy(new BigDecimal(0));
                }
                rankingVos.add(rankingVo);
            }

        }
        List<RankingVo> collect = rankingVos.stream().sorted(Comparator.comparing(RankingVo::getRateCopy).reversed()).collect(Collectors.toList());
        if (collect.size() >= 10) {
            return collect.subList(0, 10);
        }
        return collect;
    }

    @Override
    public Page<CenterPageQueryVo> centerPageQuery(Page page, CenterPageQueryPo centerPageQueryPo) {
        Page<CenterPageQueryVo> centerPageQueryVoPage = new Page<>();
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        List<DealProcessNodeEntity> dealProcessNodeEntities = dealProcessNodeService.list(Wrappers.<DealProcessNodeEntity>lambdaQuery()
                .isNotNull(DealProcessNodeEntity::getRecordCode));
        if (CollUtil.isNotEmpty(dealProcessNodeEntities)) {
            Set<String> recordCodes = dealProcessNodeEntities.stream().map(DealProcessNodeEntity::getRecordCode).collect(Collectors.toSet());
            List<DealDataEntity> dealDataEntities = new ArrayList<>();
            if (centerPageQueryPo.getState() != null && centerPageQueryPo.getState() == 1) {
                dealDataEntities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                        .in(DealDataEntity::getRecordCode, recordCodes)
                        .eq(DealDataEntity::getCreatePersonCode, user.getUserCode())
                        .eq(DealDataEntity::getState, 4));
            } else if (centerPageQueryPo.getState() != null && centerPageQueryPo.getState() == 2) {
                dealDataEntities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                        .in(DealDataEntity::getRecordCode, recordCodes)
                        .eq(DealDataEntity::getCreatePersonCode, user.getUserCode())
                        .eq(DealDataEntity::getState, 3));
            } else if (centerPageQueryPo.getState() != null && centerPageQueryPo.getState() == 3) {
                dealDataEntities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                        .in(DealDataEntity::getRecordCode, recordCodes)
                        .eq(DealDataEntity::getCreatePersonCode, user.getUserCode())
                        .eq(DealDataEntity::getState, 1));
            } else if (centerPageQueryPo.getState() == null) {
                dealDataEntities = dealDataService.list(Wrappers.<DealDataEntity>lambdaQuery()
                        .in(DealDataEntity::getRecordCode, recordCodes)
                        .eq(DealDataEntity::getCreatePersonCode, user.getUserCode()));
            }

            if (CollUtil.isNotEmpty(dealDataEntities)) {
                Set<String> codes = dealDataEntities.stream().map(DealDataEntity::getRecordCode).collect(Collectors.toSet());
                centerPageQueryPo.setCodes(codes);
                centerPageQueryVoPage = this.getBaseMapper().centerPageQuery(page, centerPageQueryPo);
            }
        }

        return centerPageQueryVoPage;
    }

    @Override
    @Transactional
    public boolean dealProcessDownload(List<DisposePageVo> disposePageVoPage, HttpServletResponse response) {
        for (DisposePageVo disposePageVo : disposePageVoPage) {
            if (disposePageVo.getState() != null && disposePageVo.getState() == 1) {
                if (disposePageVo.getNodeState() == null) {
                    disposePageVo.setStateName("待处置");
                } else if (disposePageVo.getNodeState() == 1) {
                    disposePageVo.setStateName("通过");
                } else if (disposePageVo.getNodeState() == 2) {
                    disposePageVo.setStateName("不通过");
                }
            } else if (disposePageVo.getState() != null && disposePageVo.getState() == 2) {
                disposePageVo.setStateName("通过");
            } else if (disposePageVo.getState() != null && disposePageVo.getState() == 3) {
                disposePageVo.setStateName("不通过");
            } else if (disposePageVo.getState() != null && disposePageVo.getState() == 4) {
                disposePageVo.setStateName("已结束");
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", disposePageVoPage);
        String path = "\\templates\\dealProcess.xls";
        TemplateExportParams templatePath = new TemplateExportParams(ConvertTemplatePath.convertTemplatePath(path));
        try {
            ExcelUtils.exportExcel(templatePath, map, "办事业务处置", response);
        } catch (IOException e) {
            throw new BusinessException(ExceptionTrainEnum.TEMP_ERROR);
        }
        return true;
    }


}
